<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic;


use app\common\model\Product;
use app\common\logic\BaseLogic;
use app\common\model\ProductType;
use think\facade\Db;


/**
 * Product逻辑
 * Class ProductLogic
 * @package app\adminapi\logic
 */
class ProductLogic extends BaseLogic
{


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/02/14 11:21
     */
    public static function add(array $params): bool
    {

        $productType = ProductType::findOrEmpty($params['product_type_id']);
        $product =  new Product();
        $product->year = $params['year'];
        $product->price = $params['price'];
        $product->name = $productType->name;
        $product->img = $productType->img;
        $product->num = $params['num'];
        $product->status = 1;
        $product->model_id = $params['model_id'];
        $product->product_type_id = $params['product_type_id'];
        $product->save();


        return true;
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/02/14 11:21
     */
    public static function edit(array $params): bool
    {
        $productType = ProductType::findOrFail($params['product_type_id']);
        $product =  Product::findOrFail($params['id']);
        $product->year = $params['year'];
        $product->price = $params['price'];
        $product->name = $productType->name;
        $product->img = $productType->img;
        $product->num = $params['num'];
        $product->model_id = $params['model_id'];
        $product->product_type_id = $params['product_type_id'];
        $product->save();

        return true;
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/02/14 11:21
     */
    public static function delete(array $params): bool
    {
        return Product::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2025/02/14 11:21
     */
    public static function detail($params): array
    {
        return Product::findOrEmpty($params['id'])->toArray();
    }
}